<template>
    <div class="contain-box project-box">
        <header>
            <el-button @click="up" :type="up_show ? 'primary' : ''">上级组织机构</el-button>
            <el-button @click="down" :type="down_show ? 'primary': ''">下级组织机构</el-button>
        </header>
        <div class="main-box">
            <div v-show="up_show">
                <el-form inline v-if="project_leader && project_leader.td.length == 0">
                    <el-form-item label="上级项目编号：">
                        <el-input v-model="projectNumber"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="addProjectLeader">确认</el-button>
                    </el-form-item>
                </el-form>
                <el-table v-if="project_leader && project_leader.td.length > 0" :data="project_leader.td">
                    <el-table-column v-for="item in project_leader.th" :key="item.key" :prop="item.key" :label="item.label"></el-table-column>
                    <el-table-column label="操作" width="100">
                        <template slot-scope="scope">
                            <el-button type="text" @click="unLinkProjectLeader(scope.row.id)"><i class="el-icon-delete text-danger"></i></el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div v-show="down_show">
                <div>
                    <el-button @click="addChildren({show: true})" type="primary">添加下级</el-button>
                </div>
                <el-table v-if="project_children_list" :data="project_children_list.td">
                    <el-table-column v-for="item in project_children_list.th" :key="item.key" :prop="item.key" :label="item.label"></el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button @click="cancelAsocciation(scope.row.id)">取消关联</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
        <project-children :is_add_children_show="is_add_children_show" v-on:addChildren="addChildren"></project-children>
    </div>
</template>

<script>
import project from "@/api/project.js";

import ProjectChildren from "./children.vue";

export default {
    name: "project-setting",
    components: {
        ProjectChildren
    },
    data() {
        return {
            projectNumber: undefined,
            up_show: true,
            down_show: false,
            project_children_list: undefined,
            is_add_children_show: false,
            project_leader: undefined
        };
    },
    methods: {
        up() {
            this.up_show = true;
            this.down_show = false;
        },
        down() {
            this.down_show = true;
            this.up_show = false;
        },
        getProjectChildrenList() {
            project.getProjectChildrenList().then(res => {
                this.project_children_list = res;
            });
        },
        cancelAsocciation(id) {
            this.$confirm("确认取消关联?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                project.unLinkProject({ id: id }).then(res => {
                    this.$message({
                        type: "success",
                        message: "取消关联成功!"
                    });
                    this.getProjectChildrenList();
                }).catch(err => {
                    this.$message.error(err);
                });
            }).catch(() => {
                this.$message({
                    type: "info",
                    message: "已取消删除"
                });
            });
        },
        addChildren(data) {
            this.is_add_children_show = data.show;
            if (data.refresh) {
                this.getProjectChildrenList();
            }
        },
        addProjectLeader() {
            project
                .addProjectLeader({ projectNumber: this.projectNumber })
                .then(res => {
                    this.$message({
                        type: "success",
                        message: "验证成功!"
                    });
                    this.projectNumber = undefined;
                    this.getProjectLeader();
                })
                .catch(err => {
                    this.$message.error(err);
                });
        },
        getProjectLeader() {
            project.getProjectLeader().then(res => {
                this.project_leader = res;
            }).catch(err => {
                this.$message({
                    type: 'info',
                    message: err
                });
            });
        },
        unLinkProjectLeader(id) {
            this.$confirm("确认删除?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                project.unLinkLeaderProject({id: id}).then(res => {
                    this.project_leader = undefined;
                }).catch(err => {
                    this.$message.error(err);
                });
            });
        }
    },
    mounted() {
        this.getProjectChildrenList();
        this.getProjectLeader();
    }
};
</script>

<style>
.project-box {
    min-height: 400px;
}
</style>
